package M_raw_stock_info_proc;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(day_k_lines_generate week_k_lines_generate);
#原始行情数据生成接口，传入的是解析文件
#传入的是csv文件，基本格式是
#时间(格式：yyyy/mm/dd),开盘,最高,最低,收盘,成交量
sub day_k_lines_generate
{
	#生成日K线
	my ($raw_data_csv_file) = @_;
	open(PROC_FILE,$raw_data_csv_file);
	my @day_k_line_data_lines = <PROC_FILE>;
	close(PROC_FILE);
	for(@day_k_line_data_lines) {
		chomp($_);
	}
	return @day_k_line_data_lines;
}
#周K线的生成，传入的是解析文件
#时间(格式：yyyy/mm/dd-yyyy/mm/dd),开盘,最高,最低,收盘,成交量,成交额
sub week_k_lines_generate
{
	#生成周K线
	#周K线是指以周一的开盘价，周五的收盘价，全周最高价和全周最低价来画的K线图。
	my ($raw_data_csv_file) = @_;
	open(PROC_FILE,$raw_data_csv_file);
	my @day_k_lines = <PROC_FILE>;
	close(PROC_FILE);
	#基本解析参数
	my $date = "";my $start_price = 0.0;my $max_price = 0.0;
	my $min_price = 0.0;my $end_price = 0.0;my $volume = 0;
	my $volume_price = 0;
	#日期的判断
	my $cur_workday = 0;my $old_workday = 0;
	#按照周来统计的一些基本参数
	my $week_start_date = "";my $week_end_date = "";
	my $week_start_day = 0;my $week_end_day = 0;
	my $week_start_price = 0.0;my $week_max_price = 0.0;
	my $week_min_price = 0.0;my $week_end_price = 0.0;
	my $week_volume = 0;my $week_volume_price = 0;
	#结果字符串
	my $desc_string = "";
	#结果列表
	my @week_k_lines = ();
	for(@day_k_lines) {
		chomp($_);
		if(m/^\d+\/\d+\/\d+/)  {
			#首先将完整的一周识别出来，然后按照先后顺序加入到周K线中
			($date,$start_price,$max_price,$min_price,$end_price,$volume,$volume_price) = split/\,/,$_;
			#需要获取当前是星期几
			$cur_workday = `date -d $date "+%u"`;
			chomp($cur_workday);
			#print "$cur_workday\n";
			#需要对日期做进一步的分割，然后是进行一周的统计
			#不足一周的，以该周所记录的最早一日作为这周开盘
			#价，最后一日作为收盘价其它类比。
			if($old_workday == 0) {
				#初始化记录
				#最值初始化
				$week_max_price = $max_price;
				$week_min_price = $min_price;
				#将这天的开盘价作为本周的开盘价
				$week_start_price = $start_price;
				#缓存当前的收盘价为本周最后一天
				$week_end_price = $end_price;
				#周成交量加上去
				$week_volume = $volume;
				$week_volume_price = $volume_price;
				#将这天作为这周的第一天
				$week_start_date = $date;
				$week_end_date = $date;
				#统计星期几
				#直接使用date命令来确定日期的星期分割点
				$old_workday  = $cur_workday;
			} else {
				#直接将其转换为星期几来判断
				#作为这里来说就是星期1-星期5作为分界点
				if($cur_workday < $old_workday) {
					#说明是分割点
					#构造新的字符串插入到数组中去
					$desc_string = "".$week_start_date."-".$week_end_date.",".$week_start_price.",".$week_max_price.",".$week_min_price.",".$week_end_price.",".$week_volume.",".$week_volume_price;
					push(@week_k_lines,$desc_string);
					#然后进行相关数据进行重新的初始化
					$week_volume = $volume;#对于周成交量进行初始化
					$week_volume_price = $volume_price;
					$week_start_date = $date;
					$week_end_date = $date;
					$week_start_price = $start_price;
					$week_end_price = $end_price;
					$week_max_price = $max_price;#最小值初始化
					$week_min_price = $min_price;#最大值初始化
					$old_workday  = $cur_workday;
				} else {
					#说明还是一周内
					$old_workday = $cur_workday;
					#最值进行比较
					if($week_max_price < $max_price) {
						$week_max_price = $max_price;
					}
					if($week_min_price > $min_price) {
						$week_min_price = $min_price;
					}
					#缓存当前的收盘价为本周最后一天
					$week_end_price = $end_price;
					#周成交量加上去
					$week_volume += $volume;
					$week_volume_price += $volume_price;
					#缓存当前天数为本星期最后一天
					$week_end_date = $date;
				}
			}
		}
	}
	#返回数组
	return @week_k_lines;
}
